/**
 * 
 */
package com.jolestar.yayaweibotong.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jolestar.yayaweibotong.ServiceType;
import com.jolestar.yayaweibotong.YaYaRequestToken;

/**
 * @author jolestar@gmail.com
 * 
 */
public class BindAction extends AbstractAction {

	/**
	 * 
	 */
	public BindAction() {
		super("bind");
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.jolestar.yayaweibotong.controller.WebAction#doAction(javax.servlet
	 * .http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	public String doAction(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String type = request.getParameter("service_type");
		ServiceType serviceType = ServiceType.fromName(type);
		if (serviceType == null) {
			return "redirect:/dashboard";
		} else {
			YaYaRequestToken token = serviceType.getOAuthService().request(
					request);
			request.getSession(true).setAttribute("requestToken", token);
			return "redirect:" + token.getAuthorizationURL();
		}
	}

	@Override
	public String getPath() {
		return "/" + this.getName();
	}

}
